package com.xingxing.Dao;

import com.xingxing.Bean.File;
import com.xingxing.Bean.Video;
import com.xingxing.Util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class VideoDao {
    public static boolean insert(Video video) {
        int num = 0;
        Connection conn = null;
        try {
            conn = C3P0Util.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "insert into Video values(?,?,?,?);";
            num = runner.update(conn, sql,new Object[]{
                    video.getVideoID(),
                    video.getVideoDuration(),
                    video.getUserAccount(),
                    video.getFileID()
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (num > 0) return true;
        return false;
    }

    public static ArrayList<Video> findAll() {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "SELECT Video.*,File.fileDirectory FROM Video left join File ON Video.fileID=File.fileID;";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(Video.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static ArrayList<Video> findVideoByUser(String userAccount) {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "SELECT Video.*,File.fileDirectory FROM Video left join File ON Video.fileID=File.fileID" +
                    " where Video.userAccount=?;";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(Video.class),new Object[]{userAccount});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Video find(String videoID) {
        Video video = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "SELECT Video.*,File.fileDirectory FROM Video left join File ON Video.fileID=File.fileID" +
                    " where videoID=?;";
            video = (Video) runner.query(C3P0Util.getConnection(), sql, new BeanHandler(Video.class), new Object[]{videoID});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return video;
    }

    public static boolean update(Video video){
        if (delete(video.getFileID()) && insert(video)) return true;
        else return false;
    }

    public static boolean delete(String videoID) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "delete from Video where videoID = ?";
            if (runner.update(sql, new Object[]{videoID}) > 0) return true;
            return false;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }
}